package com.vmj.natives.java.lang;

import com.vmj.heap.VMClass;
import com.vmj.heap.ClassLoader;
import com.vmj.heap.VMObject;
import com.vmj.natives.NativeMethod;
import com.vmj.natives.Registry;
import com.vmj.rtda.Frame;

/**
 * Created by yangshoujun on 2017/9/17 下午3:24.
 */
public class Thread {

    public static final java.lang.String jlClass = "java/lang/Thread";

    public static void init() {
        Registry.register(jlClass, "currentThread", "()Ljava/lang/Thread;", new NativeMethod() {
            public void execute(Frame frame) {
                ClassLoader classLoader = frame.getMethod().getClazz().getClassLoader();
                VMClass threadClass = classLoader.loadClass("java/lang/Thread");
                VMObject jThread = new VMObject(threadClass);
                VMClass threadGroupClass = classLoader.loadClass("java/lang/ThreadGroup");
                VMObject jGroup = new VMObject(threadGroupClass);
                jThread.setRefVar("group", "Ljava/lang/ThreadGroup;", jGroup);
                jThread.setIntVar("priority", 1);
                frame.getOperandStack().pushRef(jThread);
            }
        });

        Registry.register(jlClass, "setPriority0", "(I)V", new NativeMethod() {
            public void execute(Frame frame) {
                // todo
            }
        });

        Registry.register(jlClass, "isAlive", "()Z", new NativeMethod() {
            public void execute(Frame frame) {
                frame.getOperandStack().pushBoolean(false);
            }
        });

        Registry.register(jlClass, "start0", "()V", new NativeMethod() {
            public void execute(Frame frame) {
                // todo
            }
        });
    }
}
